<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="灯组名称" prop="equName">
        <el-input
          v-model="queryParams.equName"
          placeholder="请输入灯组名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <ScrollView style="flex: 1;">
      <div style="display: flex; flex-wrap: wrap;margin: 0 -10px;">
        <LightCard
          v-for="item in ammeterList"
          :key="item.id"
          style="margin:6px;flex: 0 0 25%;"
          :info="item"
          @success="getList"
        />
      </div>
    </ScrollView>
    <!-- <div style="flex: 1; background-color: #F2F3F5;overflow-y: scroll;padding: 0;">
      <div style="margin: 0 -10px;">
        <LightCard v-for="item in ammeterList" :key="item.id" style="margin:10px;float: left;" :info="item" />
      </div> -->
      <!-- <LightCard :info="item" style="margin-bottom: 20px;" /> -->
      <!-- <el-row :gutter="20">
        <el-col :md="24" :lg="12">
          <el-card class="box-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>左侧隧道（出奉节方向）</span>
              <el-button style="float: right; padding: 3px 0" type="text">当前平均照明度：2122cd/m^3</el-button>
            </div>
            <el-row :gutter="20">
              <el-col v-for="item in equList" :key="item.id" :lg="12" :md="8" :sm="12">
                <LightCard :info="item" style="margin-bottom: 20px;" />
              </el-col>
            </el-row>
          </el-card>
        </el-col>
        <el-col :md="24" :lg="12">
          <el-card class="box-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>右侧隧道（回奉节方向）</span>
              <el-button style="float: right; padding: 3px 0" type="text">当前平均照明度：2122cd/m^3</el-button>
            </div>
            <el-row :gutter="20">
              <el-col v-for="item in equList" :key="item.id" :lg="12" :md="8" :sm="12">
                <LightCard :info="item" style="margin-bottom: 20px;" />
              </el-col>
            </el-row>
          </el-card>
        </el-col>
      </el-row> -->
    <!-- </div> -->
    <!-- 添加或修改设备定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="开/关" prop="status">
          <el-switch v-model="form.status" />
        </el-form-item>
        <el-form-item label="照明度">
          <el-slider v-model="form.illuminance" :format-tooltip="formatTooltip" :max="500" />
        </el-form-item>
        <el-form-item label="开启时间">
          <el-input v-model="form.time" type="number" :min="0" placeholder="请输入数字">
            <template slot="append">单位秒(s)</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  -->
    <el-dialog :title="title" :visible.sync="open1" width="680px" append-to-body>
      <div style="display: flex;">
        <div style="width: 200px;">
          <!-- <img style="width: 200px;height:200px;" src="~@/assets/images/home/01.png"></img> -->
          <dv-border-box-8 :dur="10" style="width:200px;height:200px;" />
        </div>
        <div style="flex:1">
          <el-form ref="form" :model="form1" :rules="rules" label-width="80px" size="mini">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="灯组名称" prop="equName">{{ form1.equName }}
                  <!-- <el-input v-model="form1.equName" placeholder="请输入灯组名称" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="购入价格" prop="price">{{ form1.price }}
                  <!-- <el-input v-model="form1.price" placeholder="请输入购入价格" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="灯组类型" prop="lightType">
                  <span v-if="form1.lightType ==1 ">照明灯组</span>
                  <span v-if="form1.lightType ==2 ">交通指示灯</span>
                  <!-- <el-select v-model="form1.lightType" placeholder="请选择灯组类型" style="width:100%;">
                    <el-option label="照明灯组" value="1" />
                    <el-option label="交通指示灯" value="2" />
                  </el-select> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="安装位置" prop="locationName"> {{ form1.locationName }}
                  <!-- <el-input v-model="form1.locationName" placeholder="请输入安装位置" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="生产日期" prop="produceDate"> {{ form.produceDate }}
                  <!-- <el-date-picker
                    v-model="form.produceDate"
                    clearable
                    size="small"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择生产日期"
                    style="width:100%"
                  /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="生产厂家" prop="produceCompany"> {{ form1.produceCompany }}
                  <!-- <el-input v-model="form1.produceCompany" placeholder="请输入生产厂家" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="供应商" prop="supplier"> {{ form1.supplier }}
                  <!-- <el-input v-model="form1.supplier" placeholder="请输入供应商" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="质保期" prop="warranty"> {{ form1.warranty }}
                  <!-- <el-input v-model="form1.warranty" placeholder="请输入质保期" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="margin-bottom: 10px;" label="规格型号" prop="brand"> {{ form1.brand }}
                  <!-- <el-input v-model="form1.brand" placeholder="请输入规格型号" /> -->
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item style="margin-bottom: 10px;" label="ip地址" prop="ip"> {{ form1.ip }}
                  <!-- <el-input v-model="form1.ip" placeholder="请输入ip地址" /> -->
                </el-form-item>
              </el-col>
              <!--
            <el-col :span="12">
              <el-form-item label="类型" prop="equType">
                <el-select v-model="form.equType" placeholder="请选择类型">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </el-form-item>
            </el-col> -->
            </el-row>
          </el-form>
        </div>
      </div>
      <el-divider content-position="left">运行参数</el-divider>
      <el-form ref="form" :model="form1" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="状态" prop="status" style="margin-bottom: 4px;">
              <span v-if="form1.status1 == 1" class="text-info">运行中</span>
              <span v-else-if="form1.status1 == 2" class="text-warning">故障</span>
              <span v-else>关闭</span>
              <!-- <el-switch v-model="form.status" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="电压" style="margin-bottom: 4px;"> {{ form1.electricTension }} v
              <!-- <el-slider v-model="form1.illuminance" :format-tooltip="formatTooltip" :max="500" /> -->
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="电流" style="margin-bottom: 4px;"> {{ form1.electricCurrent }} mA
              <!-- <el-slider v-model="form1.illuminance" :format-tooltip="formatTooltip" :max="500" /> -->
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="照明度" style="margin-bottom: 4px;"> {{ form1.illuminance }} cd/m^2
              <!-- <el-slider v-model="form1.illuminance" :format-tooltip="formatTooltip" :max="500" /> -->
            </el-form-item></el-col>
        </el-row>
        <!-- <el-form-item label="开启时间">
          <el-input v-model="form.time" type="number" :min="0" placeholder="请输入数字">
            <template slot="append">单位秒(s)</template>
          </el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import LightCard from './components/lightCard.vue'
import { addEqu, delEqu, getEqu, listEqu, updateEqu } from '@/api/fjgs/equ'
// import { number } from 'echarts'
import { getTnnelSection, getDeviceBySection, deviceDetails } from '@/api/fjgs/light'
import ScrollView from '@/components/Scrollbar'
export default {
  components: {
    LightCard,
    ScrollView,
    // DeviceDetails
  },
  name: 'Equ',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备定义表格数据
      ammeterList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        // equName: null,
        // locationName: null,
        // produceCompany: null,
        // supplier: null,
        // brand: null,
        // price: null,
        // produceDate: null,
        // warranty: null,
        // ip: null,
        // lightType: null,
        // equType: 2
      },
      // 表单参数
      form: {
        status: undefined,
        illuminance: null
      },
      // 表单校验
      rules: {
      },
      form1: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatTooltip(val) {
      return val + 'cd/m^2'
    },
    /** 查询设备定义列表 */
    getList() {
      this.loading = true
      getDeviceBySection(this.queryParams).then(response => {
        response.data.ammeter.map(e => {
          e.deviceType = 'ammeter'
        })
        this.ammeterList = response.data.ammeter
        // this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.open1 = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        status: false,
        illuminance: null,
        equType: 2
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加设备定义'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEqu(id).then(response => {
        this.form = {
          id: response.data.id,
          illuminance: Number(response.data.equOperation.illuminance),
          time: 0,
          status: undefined
        }
        if (response.data.equOperation) {
          this.form.status = response.data.equOperation.status == 1
        }
        this.open = true
        this.title = '灯组控制'
      })
    },
    handleView(row) {
      this.reset()
      const id = row.id || this.ids
      getEqu(id).then(response => {
        this.form1 = response.data
        this.form1.status1 = response.data.equOperation.status
        this.form1.illuminance = response.data.equOperation.illuminance
        this.form1.electricCurrent = response.data.equOperation.electricCurrent
        this.form1.electricTension = response.data.equOperation.electricTension
        this.title = '查看详情'
        this.open1 = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEqu(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEqu(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除设备定义编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delEqu(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lc/equ/export', {
        ...this.queryParams
      }, `lc_equ.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped>
// .light_card {
//   display: inline-block;
//   position: relative;
//   background: #FFFFFF;
//   border-radius: 4px;
//   margin: 0 10px 20px 10px;
//   padding-bottom: 40px;
//   transition: box-shadow 0.3s;
//   overflow: hidden;
//   &:hover {
//     box-shadow: 0 10px 40px 0 rgba(51,119,255,0.20);
//   }
//   .light_card_tag_warp {
//     position: absolute;
//     top: 0;
//     left: 0;
//     // right: 0;
//     // left: 130px;
//     // overflow: hidden;
//     width: 44px;
//     height: 44px;
//   }
//   .light_card_tag {
//     position: absolute;
//     top: 50%;
//     left: 50%;
//     transform: translate(-50%, -50%) rotate(-45deg);
//     width: 100px;
//     font-family: PingFangSC-Regular;
//     font-weight: 600;
//     font-size: 11px;
//     text-align: center;
//     background-color: #F6F9FF;
//     color: #2B3245;
//     background-color: rgb(133,133,133,0.5);
//     z-index: 1;
//     &.err {
//       color: #F80000;
//       background-color: rgba(248, 0, 0, 0.1);
//     }
//     &.succ {
//       color:  #3cb015;
//       background-color: rgba(0, 250, 0, 0.2);
//     }
//   }
//   .light_card_bottom {
//     display: flex;
//     justify-content: space-around;
//     align-items: center;
//     position: absolute;
//     left: 0;
//     right: 0;
//     bottom: 0;
//     height: 40px;
//     box-shadow: 0 0 10px 0 rgba(51,119,255,0.5);
//     background-color: #FFFFFF;
//   }
//   .light_card_body {
//     width: 100%;
//     height: 100%;
//     padding: 10px;
//     // background-color:cadetblue ;
//     border-radius: 4px;
//     .img,img {
//       display: inline-block;
//       width: 160px;
//       height: 100%;
//       vertical-align: top;
//     }
//     .equ_info {
//       vertical-align: top;
//       width: calc(100% - 160px);
//       height: 100%;
//       display: inline-block;
//       padding-left: 10px;
//       overflow: hidden;
//       font-size: 15px;
//     }
//   }
// }

</style>
